<template>
	<view class="padding_48rpx" v-if="form">
		<view class="edit flex-start-center padding-left_30rpx">
			<input class="uni-input" v-model="form.name" placeholder="请输入打印机名字" />
		
		
		</view>
		<view class="edit flex-start-center padding-left_30rpx margin-top_16rpx">
			<input class="uni-input" v-model="form.fe_user" placeholder="请输入开发者user" />


		</view>
		<view class="edit flex-start-center padding-left_30rpx margin-top_16rpx">
			<input class="uni-input" v-model="form.fe_userkey" placeholder="请输入开发者 UKEY" />


		</view>
		<view class="edit flex-start-center padding-left_30rpx margin-top_16rpx">
			<input class="uni-input" v-model="form.fe_key" placeholder="请输入打印机识别码KEY" />


		</view>
		<view class="edit flex-start-center padding-left_30rpx margin-top_16rpx">
			<input class="uni-input" v-model="form.fe_sn" placeholder="请输入打印机编号SN" />


		</view>
		<view class="passwode margin-top_40rpx " v-if="!hiddleList">
			
			<zxz-uni-data-select v-model="value" placeholder="选择门店" :localdata="range" @selectClick="selectClick"
				@change="changeSelect"></zxz-uni-data-select>
		</view>
		<!-- <view class="edit flex-start-center padding-left_30rpx margin-top_16rpx">
			<input class="uni-input" v-model="form.store_id" placeholder="请输入店铺id" />
		</view> -->
		<view class="btnSure flex" @click="addClassification()">
			{{isUpdata?"编辑":'添加'}}
		</view>

	</view>
</template>

<script>
	import {
		object2Url
	} from '@/utils/trans';
	export default {
		components: {

		},
		data() {
			return {
				tabIndex: 4,
				changeValue: '',
				isUpdata:false,
				form: {
					name:'',
					// 开发者 user
					fe_user: '',
					//开发者 UKEY
					fe_userkey: '',
					// 打印机识别码KEY
					fe_key: '',
					//   打印机编号SN
					fe_sn: '',
					store_id: ''

				},
				imgFormData: null,
				ossInfo: null,
				upPicUrl: null,
				imgStr: [],

				storageMode: "2",
				header: null,
				token: uni.getStorageSync('login').token,
				isEdit: false,
				range:[],
				value:'',
				hiddleList:true

			}
		},
		onLoad(options) {
		this.storeListFn()
		// this.form.store_id=uni.getStorageSync('login').id
		if(uni.getStorageSync('login').store_id==0){
			// 那么是管理员账号 需要选择店铺id
			this.storeListFn()
			this.hiddleList=false
		
		}
		if (options.id) {
			  this.query(options)
		}else{
			console.log('编辑')
			this.isUpdata=false
		}
      
		},
		onShow() {
			this.$at.releaseKey('submit');
			
			// if(uni.getStorageSync('login').type==1){
			// 	// 那么是管理员账号 需要选择店铺id
			// 	this.storeListFn()
			
			// }
		},
		methods: {
            async storeListFn(){
				const res = await this.$http.get({
					url: this.$api.storeList,
					data: {
				
						page:1,
						limit:10000
					},
					headers: {
						'Authorization': `Bearer ${this.token}`
					}
				
				})
				if(res.code==0){
					console.log(res.data)
					const roleArr = res.data.list.map((item) => {
						return {
							"value": item.id,
							"text": item.name + '/' + item.real_name
						}
					})
					this.range = roleArr
					
					console.log(this.range )
				}
			},
			query(options) {
				console.log('修改')
			  this.$sun.assignObject(this.form, {
			  	...options
			  })
			  this.form.id=options.id
			  this.isUpdata=true
			 setTimeout(()=>{
				  this.value= Number(this.form.store_id)
			 },100)
              console.log(this.form)

			},
			changeSelect(e) {
				console.log('e:', e, );
				this.form.store_id=e.value
			
			},
			selectClick(e){
					console.log('e:', e, );
					this.form.store_id=e.value
			},
			addClassification() {
				this.$at.uniDoFunc({
					key: 'submit',
					success: async () => {
						this.$at.lockKey('submit');
						// 登录 需要进行判断当前账号密码 和账号归属店铺及角色是否又选择
						if (!this.form.fe_user) {
							this.$sun.toast("请输入开发者 user", 'none')
							this.$at.releaseKey('submit');
						} else if (!this.form.fe_userkey) {
							this.$sun.toast("请输入开发者 UKEY", 'none')
							this.$at.releaseKey('submit');
						} else if (!this.form.fe_key) {
							this.$sun.toast('请输入打印机识别码KEY', 'none')
							this.$at.releaseKey('submit');
						}  else if (!this.form.fe_sn) {
							this.$sun.toast('请输入打印机编号SN', 'none')
							this.$at.releaseKey('submit');
						} else if (!this.form.store_id) {
							this.$sun.toast('请输入店铺id', 'none')
							this.$at.releaseKey('submit');
						}else {


							console.log((this.form))
					        let reslist = this.isUpdata? this.$api.editPrinter:this.$api.addPrinter
							const res = await this.$http.post({
								url: reslist,
								data: {

									...this.form
								},
								headers: {
									'Authorization': `Bearer ${this.token}`
								}

							})
							console.log(res)
							if (res.code == 0) {



								// 跳转到登录页面
								this.$at.releaseKey('submit');
								this.$sun.toast(res.message, 'none')

								setTimeout(() => {
									this.$sun.navBack()
								}, 500)
							} else {
								this.$at.releaseKey('submit');
								this.$sun.toast(res.message, 'none')
							}
						}

					}
				});


				console.log(this.form)
			},

		}
	}
</script>

<style lang="less" scoped>
	.edit {

		height: 108rpx;
		background: rgb(246, 248, 255);



	}

	.btnSure {
		width: 655rpx;
		height: 76rpx;
		background: rgb(81, 142, 247);
		color: rgb(255, 255, 255);
		border-radius: 108rpx;
		font-size: 32rpx;
		font-weight: 700;
		position: fixed;
		bottom: 20rpx;
	}

	page {
		background: #fff;
	}
</style>